package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetMealDao;
import com.itheima.entity.PageResult;
import com.itheima.exceptions.BusinessException;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.utils.QiniuUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.List;

/**
 * 检查组CRUD操作业务实现类
 *
 * @author W.J.Y
 */
@Service(version = "1.0.0", interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {


    @Resource
    private SetMealDao setMealDao;

    @Resource
    private JedisPool jedisPool;

    /**
     * 新增检查套餐
     * @param setmeal 检查套餐实例
     */
    @Override
    public void add(Setmeal setmeal) throws BusinessException {
        Integer count = setMealDao.findByNameOrCodeOrHelpCode(setmeal);
        if (count > 0) {
            throw new BusinessException("已存在相同编号、助记码或者名称的检查套餐，添加失败！");
        }
        //首先添加检查套餐
        setMealDao.add(setmeal);
        //再添加检查组与检查套餐的关联
        Integer id = setmeal.getId();
        List<Integer> checkGroupIds = setmeal.getCheckGroupIds();
        if (checkGroupIds!=null){
            for (Integer checkGroupId : checkGroupIds) {
                setMealDao.setCheckGroupAndSetmeal(id, checkGroupId);
            }
        }
        //将图片名称保存到redis
        saveImgName(setmeal.getImg());
    }

    /**
     * 将图片名称保存到redis
     * @param imgName   图片名称
     */
    private void saveImgName(String imgName) {
        Jedis resource = jedisPool.getResource();
        resource.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imgName);
        resource.close();
    }

    /**
     * 删除旧的图片
     * @param imgName   旧的图片名称
     */
    private void delImgName(String imgName) {
        Jedis resource = jedisPool.getResource();
        resource.srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imgName);
        resource.close();
    }

    /**
     * 编辑检查套餐
     * @param setmeal 检查套餐实例
     */
    @Override
    public void update(Setmeal setmeal) throws BusinessException {
        //获取ID
        Integer id = setmeal.getId();
        Integer count = setMealDao.findByNameOrCodeOrHelpCode(setmeal);
        if (count > 0) {
            throw new BusinessException("已存在相同编号、助记码或者名称的检查套餐，编辑失败！");
        }
        //获取旧的setmeal对象
        Setmeal oldSetmeal = setMealDao.findById(id);
        setMealDao.update(setmeal);
        //修改图片
        String img = setmeal.getImg();
        if(img!=null&&img.length()>0){
            //添加新图片
            this.saveImgName(img);
            //删除旧图片
            this.delImgName(oldSetmeal.getImg());
        }
        //首先删除检查组的旧的关联
        setMealDao.deleteCheckGroupIdsBySetmealId(id);
        //再添加检查组与检查项的关联
        List<Integer> checkGroupIds = setmeal.getCheckGroupIds();
        for (Integer checkGroupId : checkGroupIds) {
            setMealDao.setCheckGroupAndSetmeal(id, checkGroupId);
        }
    }



    /**
     * 根据ID查询检查套餐
     *
     * @param id 检查套餐ID
     * @return 检查套餐实例
     */
    @Override
    public Setmeal findById(int id) {
        Setmeal setmeal = setMealDao.findById(id);
        return setmeal;
    }

    /**
     * 查询所有的检查套餐
     *
     * @return 检查套餐集合
     */
    @Override
    public List<Setmeal> findAll() {
        return setMealDao.findAll();
    }


    /**
     * 删除检查套餐
     *
     * @param id 检查套餐ID
     * @throws BusinessException
     */
    @Override
    public void delete(int id) throws BusinessException {
        Setmeal setmeal = setMealDao.findById(id);
        setMealDao.delete(id);
        String fileName = setmeal.getImg();
        jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
        QiniuUtils.deleteFileFromQiniu(fileName);
    }

    /**
     * 分页查询
     *
     * @param pageNum     查询页码
     * @param pageSize    每页显示条数
     * @param queryString 检查组编码、助记码或者名称
     * @return 分页查询返回结果
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        //调用dao层查询语句
        Page<Setmeal> page = setMealDao.findPage(queryString);
        //封装分页查询结果对象
        return new PageResult(page.getTotal(), page.getResult());
    }



}
